package initialize

import (
	"encoding/json"
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
	"hotel_3.27/common/config"
)

func Nacos() {

	nas := config.Config.Nacos

	clientConfig := constant.ClientConfig{
		NamespaceId:         nas.NameId, //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "debug",
	}
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      nas.Host,
			ContextPath: "/nacos",
			Port:        uint64(nas.Port),
			Scheme:      "http",
		},
	}
	client, _ := clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": serverConfigs,
		"clientConfig":  clientConfig,
	})
	content, _ := client.GetConfig(vo.ConfigParam{
		DataId: nas.DataId,
		Group:  nas.Group})

	err = json.Unmarshal([]byte(content), &config.Nas)
	if err != nil {
		return
	}
	fmt.Println("nacos初始化成功")

}
